#include<vector>
#include<unordered_map>
#include<algorithm>
using namespace std;

class Solution {
public:
    int subarraysDivByK(vector<int>& A, int K) {
        unordered_map<int, int>map;
        int count = 0;
        int res = 0;
        for (int i = 0; i < A.size(); ++i) {
            count += A[i];
            int tmp = (count % K + K) % K;
            if (map.find(tmp) != map.end()) 
                res += map[tmp];
            if (tmp == 0)
                ++res;
            ++map[tmp];
        }
        return res;
    }
};